{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "edfd3c0c-331b-47f0-9f25-d44433951923",
   "metadata": {},
   "source": [
    "Chapter 07\n",
    "# 列表生成式\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b58b3e95-09be-474c-b011-6b36a72c7d28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "# 使用列表生成式，获得1 ~ 10之间所有偶数列表\n",
    "even_numbers = [num for num in range(1, 11)  \n",
    "                if num % 2 == 0] # 一行放不下\n",
    "\n",
    "print(even_numbers) # Output: [2, 4, 6, 8, 10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5da5ade9-0487-43ae-8bd4-fc6ed4708f28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], [2, 4, 6], [3, 6, 9]]\n"
     ]
    }
   ],
   "source": [
    "# 嵌套列表生成式\n",
    "matrix = [[i * j for j in range(1, 4)] \n",
    "                 for i in range(1, 4)]\n",
    "print(matrix) \n",
    "# Output: [[1, 2, 3], [2, 4, 6], [3, 6, 9]]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
